﻿using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;
//using ESRI.ArcGIS.esriSystem;

namespace GISCommon.Business
{
    public class MapToolsHelper
    {
        public MapToolsHelper()
        { }

        public MapToolsHelper(AxMapControl vMapControl)
        {
            try
            {
                m_pMapControl = vMapControl;
                //m_pMapControl.OnExtentUpdated += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnExtentUpdatedEventHandler(m_pMapControl_OnExtentUpdated);
                m_pMapControl.OnAfterDraw += new IMapControlEvents2_Ax_OnAfterDrawEventHandler(m_pMapControl_OnAfterDraw);

                m_pPreView = new ControlsMapZoomToLastExtentBackCommandClass();
                m_pPreView.OnCreate(m_pMapControl.Object);

                m_pNextView = new ControlsMapZoomToLastExtentForwardCommandClass();
                m_pNextView.OnCreate(m_pMapControl.Object);
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用MapToolsHelper构造函数出错", ex);
            }
        }

        private AxMapControl m_pMapControl = null;
        private ICommand m_pCommand = null;
        private ICommand m_pPreView = null;
        private ICommand m_pNextView = null;

        public delegate void MyHandler();
        public event MyHandler CanPreView;
        public event MyHandler CanNextView;

        public AxMapControl MapControl
        {
            get { return m_pMapControl; }
            set { m_pMapControl = value; }
        }

        public bool CanZoomToLastExtentBack
        {
            get { return m_pPreView.Enabled; }
        }

        public bool CanZoomToNextExtentForWord
        {
            get { return m_pNextView.Enabled; }
        }

        public ICommand PreViewCommand
        {
            get { return m_pPreView; }
        }

        public ICommand NextViewCommand
        {
            get { return m_pNextView; }
        }

        public void ZoomIn()
        {
            try
            {
                m_pCommand = new ControlsMapZoomInToolClass();
                m_pCommand.OnCreate(m_pMapControl.Object);
                m_pMapControl.CurrentTool = (ITool)m_pCommand;
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用ZoomIn出错", ex);
            }
        }

        public void ZoomOut()
        {
            try
            {
                m_pCommand = new ControlsMapZoomOutToolClass();
                m_pCommand.OnCreate(m_pMapControl.Object);
                m_pMapControl.CurrentTool = (ITool)m_pCommand;
            }
            catch(Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用ZoomOut出错", ex);
            }
        }

        public void Pan()
        {
            try
            {
                m_pCommand = new ControlsMapPanToolClass();
                m_pCommand.OnCreate(m_pMapControl.Object);
                m_pMapControl.CurrentTool = (ITool)m_pCommand;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Pan出错", ex);
            }
        }

        public void FullExtent()
        {
            try
            {
                m_pMapControl.Extent = m_pMapControl.FullExtent;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用FullExtent出错", ex);
            }

        }

        public void PreView()
        {
            try
            {
                m_pPreView.OnClick();
                if (CanPreView != null)
                    CanPreView();
                if (CanNextView != null)
                    CanNextView();
                //CanNextView();
                //CanPreView();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用PreView出错", ex);
            }
        }

        public void NextView()
        {
            try
            {
                m_pNextView.OnClick();

                if (CanPreView != null)
                    CanPreView();
                if (CanNextView != null)
                    CanNextView();
                //CanPreView();
                //CanNextView();
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用NextView出错", ex);
            }
        }

        public void Cancel()
        {
            try
            {
                m_pMapControl.CurrentTool = null;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用Cancel出错", ex);
            }
        }

        private void m_pMapControl_OnAfterDraw(object sender, IMapControlEvents2_OnAfterDrawEvent e)
        {
            if(CanPreView!=null)
                CanPreView();
            if(CanNextView!=null)
                CanNextView();
        }
    }
}
